#ifndef TRAFFIC_JAM_LINUX_HPP
#define TRAFFIC_JAM_LINUX_HPP

#include "direction.hpp"
#include "quadruple.hpp"
#include <pthread.h>

/* ========
 * pthread的封装 - Linux
 * ======== */

namespace jam {

namespace mutex {

/* A dirty hack */
/* 然而Linux下这玩意儿是个union, 多多少少有点麻烦 */
struct mutex_t {
  pthread_mutex_t _mutex;
  constexpr mutex_t() : _mutex(PTHREAD_MUTEX_INITIALIZER) {
  };
};

template<direction_t>
struct type_cons {
  typedef mutex_t type;
};

typedef quadruple<type_cons> mutex_quad;
typedef mutex_quad::type mutex_quad_t;

template<mutex_quad_t &mq, direction_t dir>
inline void lock() {
  pthread_mutex_lock(&(QUAD_FIELD(mutex_quad, mq, dir)._mutex));
};

template<mutex_quad_t &mq, direction_t dir>
inline void unlock() {
  pthread_mutex_unlock(&(QUAD_FIELD(mutex_quad, mq, dir)._mutex));
};

}

}

#endif //TRAFFIC_JAM_LINUX_HPP
